﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using Be;
using WebGui.Admin;
using WebGui.BllBookingService;
using WebGui.BllLessonService;
using System.Web.UI;

namespace WebGui.User {
    /// <summary>
    /// this is used to make a new booking
    /// </summary>
    public partial class New : Page {
        /// <summary>
        /// a Booking service Client
        /// </summary>
        private BookingServiceClient _booking;
        /// <summary>
        /// a Lesson sercice Client
        /// </summary>
        private LessonServiceClient _lesson;
        /// <summary>
        /// loads the page
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event Arguments</param>
        protected void Page_Load(object sender, EventArgs e) {
            _booking = (BookingServiceClient) Session["Booking"];
            _lesson = (LessonServiceClient) Session["Lesson"];
            if(!IsPostBack) {
                DateTime start = Convert.ToDateTime(Request.QueryString["start"].ToString());
                DateTime end = Convert.ToDateTime(Request.QueryString["end"].ToString());
                TxtStart.Text = start.ToString();
                TxtEnd.Text = end.ToString();
                DdlTecher.DataSource = Roles.GetUsersInRole("kørelære");
                DdlTecher.DataBind();
            }
        }
        
        /// <summary>
        /// Closes the page and modal
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event Arguments</param>
        protected void BtnClose_Click(object sender, EventArgs e) {
            Modal.Close(this);
        }
        /// <summary>
        /// Saves the new booking to the database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(Request.QueryString["id"]);
                DateTime start = Convert.ToDateTime(TxtStart.Text);
                DateTime end = Convert.ToDateTime(TxtEnd.Text);
                double startlad = Convert.ToDouble(TxtStartLad.Text);
                double startlog = Convert.ToDouble(TxtStartLog.Text);
                double endlad = Convert.ToDouble(TxtEndLad.Text);
                double endlog = Convert.ToDouble(TxtEndLog.Text);
                string teacherId = Membership.GetUser(DdlTecher.SelectedValue).ProviderUserKey.ToString();
                BeBooking bebooking = new BeBooking(start, end, teacherId);
                BeLesson lesson = _lesson.LessonPresents(bebooking);
                BeBooking booking = new BeBooking(start, end, startlad, startlog, endlad, endlog, Membership.GetUser().ProviderUserKey.ToString(), teacherId, lesson.Id);
                _booking.SaveBooking(booking);
                Modal.Close(this, "OK");
            }
            catch (Exception ex) { Response.Write("<script type='text/javascript'>alert('" + ex.Message + "'); </script>"); }
        }
    }
}